import tkinter as tk
import tkinter.font as tkFont
import tkinter.messagebox as tkMsgBox
import json


with open(file='./questions.json', mode='r', encoding='utf8') as f:
    ftext = f.read()
data = json.loads(ftext)


top = tk.Tk()
top.geometry('640x1201')
top.resizable(False,True)
name = data['title']
top.title(name)
question_info = data['questions']
question_list = []
right_answers = data['answer']
option_list = []
rightnum = 0
v_list = []


def apppend():
    global question_info,question_list,option_list
    for i in question_info:
        question_list.append(i[0])
        option_list.append(i[1].split(':'))


def addtopic(topic):
    global top
    question_frame = tk.Frame(top)
    question_frame.pack(side=tk.TOP)
    question = tk.Label(question_frame, text=topic, anchor=tk.W)
    question.pack(side=tk.TOP, fill=tk.X)


opt_list = []
def giveval():
    global opt_list,v_list
    val_iter = 1
    it = 0
    for i in option_list:
        opts = []
#        question_frame = tk.Frame(top)
#        question_frame.pack(side=tk.TOP)
#        question = tk.Label(question_frame, text=i, anchor=tk.W)
#        question.pack(side=tk.TOP, fill=tk.X)
        addtopic(question_list[it])
        v = i
        v = tk.IntVar()
        v_list.append(v)
        v.set(0)
        it += 1
        for j in range(0,len(i)):
            opts.append(tk.Radiobutton(top, text=i[j], variable=v, value=val_iter))
            opts[j].pack(side = tk.TOP)
            val_iter += 1
        opt_list.append(opts)


def check():
    global opt_list,right_answers,rightnum
    ans = []
    j = 0
    for i in opt_list:
        if v_list[j].get() == 0:
            tkMsgBox.showerror('error', "你还未作答第"+str(j+1)+"题")
            return
        if j < len(v_list) - 1:
            ans.append(v_list[j].get())
            j += 1
    for i in range(0,len(ans)):
        if ans[i] == right_answers[i]:
            rightnum += 1
    popup()


def submit():
    global top
#    submit_frame = tk.Frame(top)
#    submit_frame.pack(side = tk.BOTTOM) 
    submit = tk.Button(top, text='提交', command=check)
    submit.pack(side = tk.TOP)

'''
def addquestions():
    global question_list,opt_list
    question_frame = tk.Frame(top)
    question_frame.pack(side=tk.TOP, fill=tk.X)
    question = 0
    question = tk.Label(question_frame, text=question_list, anchor=tk.W)
    question.pack(side=tk.TOP, fill=tk.X)
    option_frame = tk.Frame(question_frame)
    option_frame.pack(side=tk.TOP, fill=tk.X)
    submit()
#    for i in opt_list:
#        i.pack(side=tk.LEFT)
'''

def popup():
    global rightnum
    if rightnum == 0:
        tkMsgBox.showinfo(':{', "你对沈岱林一无所知,1题都没有答对")
        return
    elif rightnum < 3:
        tkMsgBox.showinfo(':(', "你对沈岱林的了解甚少,只答对了"+str(rightnum) +"题")
        return
    elif rightnum < 6:
        tkMsgBox.showinfo(':|', "你对沈岱林的了解不多,答对了"+str(rightnum) +"题")
        return
    elif rightnum < 9:
        tkMsgBox.showinfo(':)', "你对沈岱林的了解比他所预料的要多的多,竟然答对了"+str(rightnum) +"题")
        return
    tkMsgBox.showinfo(':}', "沈岱林:你是谁,你是如何做到如此了解我的,我很想知道,全对!")
        

def main():
    apppend()
    giveval()
    submit()
    
    
main()